//
//  StatusIndicatorHelper.m
//  Meal4iOS
//
//  Created by Porcellio on 8/23/12.
//
//

#import "StatusIndicatorHelper.h"

#import "StatusIndicatorView.h"

@implementation StatusIndicatorHelper

static StatusIndicatorView *indicatorView = nil;
static UIView *backgroundView = nil;

+ (void)showIndicatorInView:(UIView *)view withText:(NSString *)text {
    
    if(indicatorView == nil) {
        
        indicatorView = [[StatusIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 400.0f, 80.0f)];
        indicatorView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
        backgroundView.userInteractionEnabled = YES;
        indicatorView.center = backgroundView.center;
        [backgroundView addSubview:indicatorView];
    }

    indicatorView.statusText = text;
    
    [indicatorView.processBar start];
    
    if(view == nil) {
    
        UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];
        backgroundView.frame = window.bounds;
        [window addSubview:backgroundView];
        [window bringSubviewToFront:backgroundView];
    }else {
    
        backgroundView.frame = view.bounds;
        [view addSubview:backgroundView];
        [view bringSubviewToFront:backgroundView];
    }
}

+ (void)hiddenIndicatorInView:(UIView *)view {

    if(indicatorView == nil) {
    
        return;
    }
    
    [UIView animateWithDuration:0.5f
                     animations:^{
                         
                         backgroundView.alpha = 0.0f;
                     } completion:^(BOOL finished) {
                         
                         [backgroundView removeFromSuperview];
                         backgroundView.alpha = 1.0f;
                         
                     }];
    [indicatorView.processBar stop];
}

@end
